﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace miniword
{

    public partial class Form1 : Form
    {
        private string file_name = "";

        private bool modified;

        static int st = 0;
        static string fs = "";

        public void set_file_name(string fn)
        {
            file_name = fn;
        }

        public string get_file_name()
        {
            return file_name;
        }

        public Form1(MDIParent1 pa, string file_name)
        {
            InitializeComponent();
            this.MdiParent = pa;

            this.richTextBox1.LoadFile(file_name, RichTextBoxStreamType.RichText);
            this.Text = file_name;
            this.modified = false;

        }

        public Form1()
        {
            InitializeComponent();
        }

        public void save(string file_name)
        {
            if (file_name == "")
            {
                richTextBox1.SaveFile(this.file_name, RichTextBoxStreamType.RichText);
                this.Text = this.file_name;
            }
            else
            {
                this.file_name = file_name;
                richTextBox1.SaveFile(this.file_name, RichTextBoxStreamType.RichText);
                this.Text = this.file_name;
            }
        }

        public void save_as(string file_name)
        {
            this.file_name = file_name;
            richTextBox1.SaveFile(this.file_name);
            this.Text = this.file_name;
        }

        public void redo()
        {
            if(richTextBox1.CanRedo)
                richTextBox1.Redo();
            return;
        }

        public void undo()
        {
            if (richTextBox1.CanUndo)
                richTextBox1.Undo();
            return;
        }

        public void selectAll()
        {
            this.richTextBox1.SelectAll();
        }

        public void search(string s)
        {
            if (s != fs)
            {
                fs = s;
                st = 0;
            }

            int now = this.richTextBox1.Find(s, st, this.richTextBox1.Text.Length, RichTextBoxFinds.NoHighlight);
            if(now > 0)
            {
                this.richTextBox1.Select(now,s.Length);
                this.Focus();
                st = now + s.Length;
            }
            else
            {
                st = 0;
                MessageBox.Show("已搜索到结尾，或者没有找到相应字符");
            }       
        }

        public void printPreview()
        {
            printPreviewDialog1.Document = this.printDocument1;
            printPreviewDialog1.ShowDialog();
        }

        public void print()
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }
    }
}
